package su.product.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.product.dao.IProductPriceDAO;
import su.product.entity.ProductPrice;


@Repository
public class ProductPriceDAO extends SuBaseDAO implements IProductPriceDAO {
	
	
	public List<ProductPrice> queryByPage(PagerDTO s) throws SQLException {
		String sql = "select * from su_product_price where status=1";
		if(StringUtils.isNotBlank(s.getS1())){//id
			if(s.getS1().indexOf(",")>-1){
				sql += " and id in ("+s.getS1()+")";
			}else{
				sql += " and id="+s.getS1();
			}
		}
		if(StringUtils.isNotBlank(s.getS2())){//
			sql += " and jianpin like '%"+s.getS2()+"%'";
		}
		sql += " ORDER BY id desc";
		
		return super.queryByPage(sql, new RowMapper<ProductPrice>() {

			public ProductPrice mapRow(ResultSet rs, int arg1) throws SQLException {
				ProductPrice entity = new ProductPrice();
				entity.setId(rs.getInt("id"));
				entity.setPid(rs.getInt("pid"));
				entity.setTimefrom(rs.getTimestamp("timefrom"));
				entity.setTimeto(rs.getTimestamp("timeto"));
				entity.setPricefrom(rs.getDouble("pricefrom"));
				entity.setPriceto(rs.getDouble("priceto"));
				entity.setPayment(rs.getDouble("payment"));
				entity.setStatus(rs.getInt("status"));
				return entity;
			}
			
		}, s);
	}
	
	
	public List<ProductPrice> getPriceByProductId(int productId) throws SQLException {
		String sql = "select * from su_product_price where status=1 and pid=? order by id";
		
		return super.query(sql,new Object[]{productId}, new RowMapper<ProductPrice>() {

			public ProductPrice mapRow(ResultSet rs, int arg1) throws SQLException {
				ProductPrice entity = new ProductPrice();
				entity.setId(rs.getInt("id"));
				entity.setPid(rs.getInt("pid"));
				entity.setTimefrom(rs.getTimestamp("timefrom"));
				entity.setTimeto(rs.getTimestamp("timeto"));
				entity.setPricefrom(rs.getDouble("pricefrom"));
				entity.setPriceto(rs.getDouble("priceto"));
				entity.setPayment(rs.getDouble("payment"));
				entity.setStatus(rs.getInt("status"));
				return entity;
			}
			
		});
	}

	public List<ProductPrice> getAllPrice(ProductPrice p) throws SQLException{
		String sql = "select * from su_product_price where pid=? and status=1 and (timeto is NULL or timeto>?)";
		return super.query(sql,new Object[]{p.getPid(),p.getTimetoStr()}, new RowMapper<ProductPrice>() {

			public ProductPrice mapRow(ResultSet rs, int arg1) throws SQLException {
				ProductPrice entity = new ProductPrice();
				entity.setId(rs.getInt("id"));
				entity.setPid(rs.getInt("pid"));
				entity.setTimefrom(rs.getTimestamp("timefrom"));
				entity.setTimeto(rs.getTimestamp("timeto"));
				entity.setPricefrom(rs.getDouble("pricefrom"));
				entity.setPriceto(rs.getDouble("priceto"));
				entity.setPayment(rs.getDouble("payment"));
				entity.setStatus(rs.getInt("status"));
				return entity;
			}
			
		});
	}
	
	
	public ProductPrice getById(int id) throws SQLException {
		String sql = "select * from su_product_price where status=1 and id=? limit 1";
		return super.queryForObject(sql, new Object[]{id}, new RowMapper<ProductPrice>() {
			public ProductPrice mapRow(ResultSet rs, int value) throws SQLException {
				ProductPrice entity = new ProductPrice();
				entity.setId(rs.getInt("id"));
				entity.setPid(rs.getInt("pid"));
				entity.setTimefrom(rs.getTimestamp("timefrom"));
				entity.setTimeto(rs.getTimestamp("timeto"));
				entity.setPricefrom(rs.getDouble("pricefrom"));
				entity.setPriceto(rs.getDouble("priceto"));
				entity.setPayment(rs.getDouble("payment"));
				entity.setStatus(rs.getInt("status"));
				return entity;
			}
		});
	}

	
	public void remove(int id) throws SQLException {
		final String sql = "update su_product_price set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
	
	
	
	public void insert(ProductPrice o) throws SQLException {
		String sql = "insert into su_product_price(pid ,timefrom ,timeto ,pricefrom ,priceto ,payment ,status ) values(? ,? ,? ,? ,? ,? ,? )";
		super.update(sql, new Object[]{o.getPid() ,o.getTimefrom() ,o.getTimeto() ,o.getPricefrom() ,o.getPriceto() ,o.getPayment() ,o.getStatus() });
	}
	
	
	
	public void update(ProductPrice o) throws SQLException {
		StringBuffer updateSql = new StringBuffer("update su_product_price set ");
		List<Object> args = new ArrayList<Object>();
		args.add(o.getPid());
		updateSql.append("pid=?").append(",");
		args.add(o.getTimefrom());
		updateSql.append("timefrom=?").append(",");
		args.add(o.getTimeto());
		updateSql.append("timeto=?").append(",");
		args.add(o.getPricefrom());
		updateSql.append("pricefrom=?").append(",");
		args.add(o.getPriceto());
		updateSql.append("priceto=?").append(",");
		args.add(o.getPayment());
		updateSql.append("payment=?").append(",");
		args.add(o.getStatus());
		updateSql.append("status=?").append(",");
			
		
		args.add(o.getId());
			
		
		String sql = updateSql.substring(0, updateSql.length()-1) + " where id = ? "; 
		super.update(sql, args.toArray());
	}

}

